﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_SalaryGroup : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IPositionTypeBLL posiTypeBLL = new PositionTypeBLL();
        private IMasRecordStatusBLL recStaBLL = new MasRecordStatusBLL();
        private SalaryGroupBLL salaryGroupBLL = new SalaryGroupBLL();
        private IClassBLL classBLL = new ClassBLL();
        Main main = new Main();

        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindEmpType();
                bindStaRec();
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "sal_grp_seq").ToString();
                    ddlEmployeeType.SelectedValue = DataBinder.Eval(DataItem, "emp_type_seq").ToString();
                    bindPositionType();
                    ddlPosiType.SelectedValue = DataBinder.Eval(DataItem, "pos_type_seq").ToString();
                    bindClass();
                    ddlClass2.SelectedValue = DataBinder.Eval(DataItem, "class_seq").ToString();
                    ddlStatusRec.SelectedValue = DataBinder.Eval(DataItem, "record_status").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                }
                else
                {
                    ddlEmployeeType.SelectedValue = Session["EMP_TYPE_SEQ"].ToString();
                    bindPositionType();
                }

                ddlEmployeeType.Enabled = !(DataItem is Telerik.Web.UI.GridInsertionObject);

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                bool chkDup;
                chkDup = salaryGroupBLL.ChkDuplicate(int.Parse(ddlEmployeeType.SelectedValue), int.Parse(ddlPosiType.SelectedValue), int.Parse(ddlClass2.SelectedValue), ddlStatusRec.SelectedValue);

                if (chkDup != true)
                    salaryGroupBLL.Insert(int.Parse(ddlEmployeeType.SelectedValue), int.Parse(ddlPosiType.SelectedValue), int.Parse(ddlClass2.SelectedValue), txtRemarks.Text, ddlStatusRec.SelectedValue, ssMng.EmpSeq);
                else
                    main.MessageAlert(this.Page, "Duplicate!!");
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                bool chkDup = salaryGroupBLL.ChkDuplicate(int.Parse(ddlEmployeeType.SelectedValue), int.Parse(ddlPosiType.SelectedValue), int.Parse(ddlClass2.SelectedValue), ddlStatusRec.SelectedValue, string.IsNullOrEmpty(lblID.Text) ? (int?) null : Convert.ToInt32(lblID.Text));

                if (chkDup != true)
                    salaryGroupBLL.Update(int.Parse(lblID.Text), Convert.ToInt32(ddlEmployeeType.SelectedValue), Convert.ToInt32(ddlPosiType.SelectedValue), Convert.ToInt32(ddlClass2.SelectedValue), txtRemarks.Text, ddlStatusRec.SelectedValue, ssMng.EmpSeq);
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "MessageAlert('ไม่สามารถเพิ่มข้อมูลได้ เนื่องจากมีข้อมูลซ้ำ')", true);
                    return;
                }
                   // Utils.Web.ShowAlert(this.Page, "Duplicate!!");
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void ddlEmployeeType_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bindPositionType();
        }

        protected void ddlPosiType_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            bindClass();
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
            ddlEmployeeType.DataSource = infoList;
            ddlEmployeeType.DataBind();
        }
        private void bindPositionType()
        {
            try
            {
                ddlClass2.Items.Clear();
                ddlClass2.Text = string.Empty;

                DataSet ds = posiTypeBLL.LoadPositionTypes((int?)null, int.Parse(ddlEmployeeType.SelectedValue));
                //IList<PositionTypeInfo> infoList = posiTypeBLL.ListPositionTypes(int.Parse(ddlEmployeeType.SelectedValue));
                if (ds.Tables.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    ddlPosiType.DataSource = dt;
                    ddlPosiType.DataBind();
                }
                else
                {
                    ddlPosiType.Items.Clear();
                    ddlPosiType.Text = string.Empty;

                }
            }
            catch (Exception ex)
            {

            }
        }
        private void bindClass()
        {
            Nullable<int> nullPositionType = string.IsNullOrEmpty(ddlPosiType.SelectedValue) ? (int?)null : Int32.Parse(ddlPosiType.SelectedValue);
            Nullable<int> nullEmloyeeType = string.IsNullOrEmpty(ddlEmployeeType.SelectedValue) ? (int?)null : Int32.Parse(ddlEmployeeType.SelectedValue);
            
            ddlClass2.Text = string.Empty;
            DataSet ds = classBLL.LoadClasses((int?)null, nullPositionType, nullEmloyeeType);

            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                ddlClass2.DataSource = ds;
                ddlClass2.DataBind();
            }
        }
        private void bindStaRec()
        {
            IList<MasRecordStatusInfo> InfoList = recStaBLL.ListRecordStatus();
            ddlStatusRec.DataSource = InfoList;
            ddlStatusRec.DataBind();
        }
    }
}